export default {
    init:function(routerUrl,data,flag = false){
        if(!routerUrl) {
            return '';
        }
        var routerUrlArr = routerUrl.split("?");
        if(routerUrlArr.length < 2) {
            return {path:routerUrl,query:{}};
        }
        var obj = {};
        if(flag == true) {
            if (routerUrlArr[0].substring(0, 1) == '/') {
                obj.path = this.urlExpression(routerUrlArr);//routerUrlArr[0];
            } else {
                obj.path = '/' + this.urlExpression(routerUrlArr);//routerUrlArr[0];
            }
        }else{
            if (routerUrlArr[0].substring(0, 1) == '/') {
                obj.path = routerUrlArr[0];
            } else {
                obj.path = '/' + routerUrlArr[0];
            }
        }
        
   
        if(Object.prototype.toString.call(data) == '[object Object]') {
            obj.query = this.objectExpression(routerUrlArr[1],data);
        }else if(Object.prototype.toString.call(data) == '[object Array]') {
            obj.query = this.arrayExpression(routerUrlArr[1],data);
        }

        return obj;
    },
    arrayExpression:function(routerArr,data){
        var query = {};
        var arrPara = routerArr.split("&");
        arrPara.forEach(item => {
            var  arr = item.split("=");   
            if(arr.length > 1) {
                if (arr[1] == '###') {
                    var param = [];
                    console.log(data);
                    for (var j in data) {
                        param.push(data[j][arr[0]]);
                    }
                }else {
                    var param = arr[1];
                }

                query[arr[0]] = param;
            }
        });

        return query
    },
    objectExpression:function(routerArr,data){
        var query = {};
        var arrPara = routerArr.split("&");
        arrPara.forEach(item => {
            var arr = item.split("=");   
            if(arr.length > 1) {
                if (arr[1] == '###') {
                    for (var j in data) {
                        query[arr[0]] = data[arr[0]];
                    }
                }else {
                    query[arr[0]] = arr[1];
                }
            }
        });

        return query
    },
    urlExpression:function(routerArr) {
        var query = [];
        var arrPara = routerArr[1].split("&");
        arrPara.forEach(item => {
            var arr = item.split("=");   
            if(arr.length > 1 && arr[1] != '###') {
                query.push(item);
            }
        })

        if(query.length) {
            return routerArr[0] + '?' + query.join('&');
        }else{
            return routerArr[0];
        }
       
    }
}